// use hive to save data
import 'package:flutter/material.dart';
import 'package:hive_flutter/adapters.dart';

class Page5 extends StatefulWidget {
  const Page5({super.key});

  @override
  State<Page5> createState() => _Page5State();
}

class _Page5State extends State<Page5> {
  var box = Hive.box('myBox');
  TextEditingController _nameController = TextEditingController();
  addDate(String name) async {
    final key = DateTime.now().millisecondsSinceEpoch.toString();
    //添加数据
    box.put(key, name);
  }

  getDate() async {
    //获取数据
    // String name = box.get('name');
    for (int i = 0; i < box.length; i++) {
      var key = box.keyAt(i);
      var value = box.getAt(i);
      print('Key: $key, Value: $value');
    }
  }

  void dispose() {
    _nameController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Page 5')),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            SizedBox(height: 20),
            TextField(
              controller: _nameController,
              keyboardType: TextInputType.text,
              decoration: InputDecoration(
                labelText: 'Enter name',
                border: OutlineInputBorder(),
              ),
            ),
            SizedBox(height: 20),
            ElevatedButton(
              onPressed: () {
                addDate(_nameController.text);
                _nameController.clear(); // 清空输入框
                ScaffoldMessenger.of(context).showSnackBar(
                  SnackBar(content: Text('Data added successfully!')),
                );
              },
              child: Text('Add Data'),
            ),
            ElevatedButton(
              onPressed: () {
                getDate();
              },
              child: Text('Get Data'),
            ),
            SizedBox(height: 20),
            Expanded(
              child: ValueListenableBuilder(
                valueListenable: box.listenable(),
                builder: (context, box, widget) {
                  return ListView.builder(
                    // shrinkWrap: true,
                    itemCount: box.length,
                    itemBuilder: (context, index) {
                      var key = box.keyAt(index);
                      var value = box.getAt(index);
                      return Material(
                        color: Colors.blueGrey,
                        borderRadius: BorderRadius.circular(10),
                        elevation: 5,
                        child: ListTile(
                          title: Text('Key: $key, Value: $value'),
                          trailing: IconButton(
                            icon: Icon(Icons.delete),
                            color: Colors.red,
                            onPressed: () {
                              box.delete(key);
                            },
                          ),
                        ),
                      );
                    },
                  );
                },
              ),
            ),
          ],
        ),
      ),
    );
  }
}
